using MediatR;
using SmartApplication.Api.Applient.Command.Process;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Process
{
    public class UpdateProcessHandler : IRequestHandler<UpdateProcessCommand, ApiResult<int>>
    {
        public UpdateProcessHandler(IBaseRepository<ProcessingModel> processRepository)
        {
            ProcessRepository = processRepository;
        }

        public IBaseRepository<ProcessingModel> ProcessRepository { get; }

        /// <summary>
        /// 修改深加工信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateProcessCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();

            var process = await ProcessRepository.GetValueAsync(request.Id);
            process.SourceId = request.SourceId;
            process.Team = request.Team;
            process.ChargeName = request.ChargeName;
            process.FoodSafetyId = request.FoodSafetyId;
            process.CorporateId = request.CorporateId;

            var update = await ProcessRepository.UpdateAsync(process);
            if (update > 0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改深加工信息成功";
                res.Data = update;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改深加工信息失败";
                res.Data = update;
                return res;
            }
        }
    }
}